package Client端;

import javax.swing.*;
import javax.swing.border.Border;
import javax.swing.border.TitledBorder;
import javax.swing.event.DocumentEvent;
import javax.swing.event.DocumentListener;
import java.awt.*;
import java.util.Arrays;


public class ClientFrame extends JFrame {
    public String id, name;
    public String receiver = null;
    public JTextArea messageArea;
    public JLabel number;
    public JTextArea write;
    public JButton createGroupBtn;
    public JButton addGroupBtn;
    public JButton addFriendBtn;
    public JButton exitBtn;
    public messageThread renewThread = null;
    public String[][] friendNames;
    public MyButton[] friendButtons;

    public JPanel friend_content = new JPanel();
    public String[][] groupNames;
    public MyButton[] groupButtons;
    public JPanel group_content;
    public inputFrame1 input1 = new inputFrame1();
    public inputFrame2 input2 = new inputFrame2();
    public Font font_20 = new Font("", Font.PLAIN, 20);

    public ClientFrame(String id, String name) {
        setResizable(false);
        this.id = id;
        this.name = name;
        setSize(900, 560);
        setLocationRelativeTo(null);
        setTitle("简易聊天室客户端");
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

        add(leftPart(), BorderLayout.WEST);
        add(centerPart(), BorderLayout.CENTER);
        add(rightPart(), BorderLayout.EAST);
        setVisible(true);
        Object.clientFrame = this;
    }

    public JSplitPane leftPart() {
        JSplitPane left_part = new JSplitPane(JSplitPane.VERTICAL_SPLIT);
        left_part.setPreferredSize(new Dimension(200, 560));
        left_part.setDividerLocation(0.5);//设置分隔条位置
        left_part.setBorder(BorderFactory.createMatteBorder(0, 0, 0, 1, Color.gray));
        //设置边框
        JScrollPane friendScroll = new JScrollPane();//设置滚动面板
        friendScroll.setBorder(BorderFactory.createTitledBorder(
                BorderFactory.createMatteBorder(2, 2, 2, 2,
                        new Color(124, 124, 241, 176)),
                "好友栏", TitledBorder.CENTER, TitledBorder.DEFAULT_POSITION,
                font_20, Color.BLACK));
        friendScroll.setHorizontalScrollBarPolicy(JScrollPane.HORIZONTAL_SCROLLBAR_NEVER);
        friendScroll.setPreferredSize(new Dimension(200, 250));
        //设置friend边框
        friend_content.setLayout(new FlowLayout(FlowLayout.LEFT, 1, 0));
        renewFriends();
        friendScroll.setViewportView(friend_content);
        left_part.add(friendScroll);

        JScrollPane groupScoll = new JScrollPane();
        groupScoll.setBorder(BorderFactory.createTitledBorder(
                BorderFactory.createMatteBorder(2, 2, 2, 2, new Color(124, 124, 241, 176)),
                "群聊栏", TitledBorder.CENTER, TitledBorder.DEFAULT_POSITION,
                font_20, Color.BLACK));
        groupScoll.setHorizontalScrollBarPolicy(JScrollPane.HORIZONTAL_SCROLLBAR_NEVER);
        groupScoll.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED);
        group_content = new JPanel();
        group_content.setLayout(new FlowLayout());
        renewGroups();
        groupScoll.setViewportView(group_content);
        left_part.add(groupScoll);
        return left_part;
    }

    public JPanel centerPart() {
        JPanel center_part = new JPanel();
        center_part.setLayout(null);
        JScrollPane messageScroll = new JScrollPane();
        messageScroll.setBounds(0, 0, 490, 340);
        messageScroll.setBorder(BorderFactory.createMatteBorder(0, 0, 1, 0, Color.black));
        messageArea = new JTextArea();
        messageArea.setFont(font_20);
        messageArea.setEditable(false);
        messageScroll.setViewportView(messageArea);
        center_part.add(messageScroll);

        JScrollPane writeScroll = new JScrollPane();
        writeScroll.setBounds(0, 340, 490, 150);
        write = new JTextArea();
        write.setLineWrap(true);
        write.setWrapStyleWord(true);
        write.setOpaque(false);
        writeScroll.setViewportView(write);
        center_part.add(writeScroll);

        JButton sendBtn = new JButton("发送");
        sendBtn.setBounds(400, 490, 80, 30);
        center_part.add(sendBtn);

        number = new JLabel();
        number.setBounds(300, 490, 60, 30);
        number.setHorizontalTextPosition(SwingConstants.RIGHT);
        number.setText("0/100");
        center_part.add(number);

        JLabel alert = new JLabel();
        alert.setBounds(180, 490, 100, 30);
        center_part.add(alert);
        write.getDocument().addDocumentListener(new DocumentListener() {
            @Override
            public void insertUpdate(DocumentEvent e) {
                renewNumber();
            }

            @Override
            public void removeUpdate(DocumentEvent e) {
                renewNumber();
            }

            @Override
            public void changedUpdate(DocumentEvent e) {
                renewNumber();
            }
        });
        sendBtn.addActionListener(e -> {
            String text = write.getText();
            if (text.length() > 100) {
                alert.setText("字符串过长");
            } else if (receiver == null) {
                alert.setText("未选择接受者");
            } else {
                ClientDeal.send(id, receiver, text);
            }
        });
        return center_part;
    }

    public JPanel rightPart() {
        JPanel right_part = new JPanel();
        right_part.setLayout(null);
        right_part.setPreferredSize(new Dimension(200, 560));
        right_part.setBorder(BorderFactory.createMatteBorder(0, 1, 0, 0, Color.black));

        JPanel information = new JPanel();
        information.setBounds(10, 20, 180, 100);
        Border border = BorderFactory.createTitledBorder(
                BorderFactory.createLineBorder(new Color(119, 152, 236, 152), 2, true),
                "个人信息", TitledBorder.CENTER, TitledBorder.DEFAULT_POSITION,
                new Font("", Font.PLAIN, 20));
        information.setBorder(border);
        information.setLayout(new GridLayout(2, 1));
        JLabel info_name = new JLabel(" 名字： " + name);
        info_name.setFont(font_20);
        information.add(info_name);
        JLabel info_id = new JLabel(" 账号： " + id);
        info_id.setFont(font_20);
        information.add(info_id);
        right_part.add(information);

        exitBtn = new JButton("退出登录");
        exitBtn.setBounds(25, 300, 150, 35);
        exitBtn.setFont(font_20);
        right_part.add(exitBtn);
        exitBtn.addActionListener(e -> {
            new LoginMain();
            this.dispose();
            if (renewThread != null) {
                renewThread.endThread();
                renewThread = null;
            }
        });

        addFriendBtn = new JButton("添加好友");
        addFriendBtn.setBounds(25, 350, 150, 35);
        addFriendBtn.setFont(font_20);
        right_part.add(addFriendBtn);
        addFriendBtn.addActionListener(e -> input1.wake("好友号", id));

        addGroupBtn = new JButton("加入群聊");
        addGroupBtn.setBounds(25, 400, 150, 35);
        addGroupBtn.setFont(font_20);
        right_part.add(addGroupBtn);
        addGroupBtn.addActionListener(e -> input1.wake("群聊号", id));

        createGroupBtn = new JButton("创建群聊");
        createGroupBtn.setBounds(25, 400, 150, 35);
        createGroupBtn.setFont(font_20);
        right_part.add(createGroupBtn);
        createGroupBtn.addActionListener(e -> input2.wake());

        return right_part;
    }

    public void renewFriends() {
        friend_content.removeAll();//清空所有组件
        friendNames = ClientDeal.getFriend(id);//获取id的好友
        friendButtons = new MyButton[friendNames.length];
        for (int i = 0; i < friendNames.length; i++) {
            int finalI = i;
            friendButtons[i] = new MyButton(friendNames[i][1] + "(" + friendNames[i][0] + ")");
            friendButtons[i].addActionListener(e -> setReceiver(friendNames[finalI][1]));
            friend_content.add(friendButtons[i]);
        }
        friend_content.setPreferredSize(new Dimension(200, friendNames.length * 31));
        System.out.println(Arrays.deepToString(friendNames));
        setVisible(false);
        setVisible(true);
    }

    public void renewGroups() {
        group_content.removeAll();//清空所有组件
        groupNames = ClientDeal.getGroup(id);//获取id的群聊
        groupButtons = new MyButton[groupNames.length];
        for (int i = 0; i < groupNames.length; i++) {
            int finalI = i;
            groupButtons[i] = new MyButton(groupNames[i][1] + "(" + groupNames[i][0] + ")");
            groupButtons[i].addActionListener(e -> setReceiver(groupNames[finalI][1]));
            group_content.add(groupButtons[i]);
            groupButtons[i].repaint();
        }
        group_content.setPreferredSize(new Dimension(200, groupNames.length * 31));
        System.out.println(Arrays.deepToString(groupNames));
        Thread.yield();
        setVisible(false);
        setVisible(true);
    }

    public void renewNumber() {
        int length = write.getText().length();
        number.setText(length + "/100");
        if (length > 100) number.setForeground(Color.red);
        else number.setForeground(Color.BLACK);
    }

    public void setReceiver(String receiver) {//更新
        this.receiver = receiver;
        if (renewThread != null) renewThread.endThread();
        System.out.println(receiver);
        renewThread = new messageThread(id, receiver, messageArea);
        renewThread.start();
    }
}

class MyButton extends JButton {
    public static final Font font_20 = new Font("", Font.PLAIN, 20);

    public MyButton(String name) {
        super(name);
        setFont(font_20);
        setPreferredSize(new Dimension(180, 30));
    }
}